package base.synchronizedstudy;

import java.util.LinkedList;

public class ThreaPoolStudy {
	int threadSize;//大小
	LinkedList<Runnable> tasks = new LinkedList<>();//人物容器
	//试图消费任务的线程
	public ThreaPoolStudy(){
		threadSize = 10;
		synchronized(tasks){
			for(int i=0;i<threadSize;i++){
				new TaskConsumeThread("任务消费者线程"+i).start();
			}
		}
	}
	public void add (Runnable r){
		synchronized(tasks){
			tasks.add(r);
			tasks.notifyAll();//
		}
	}
	class TaskConsumeThread extends Thread{
		public TaskConsumeThread(String name){
			super(name);
		}
		Runnable task;
		public void run(){
			System.out.println("启动"+this.getName());
			while(true){
				synchronized(tasks){
					while(tasks.isEmpty()){
						try {
							tasks.wait();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					task = tasks.removeLast();
					tasks.notifyAll();
				}
				System.out.println(this.getName()+"获取到人物并执行");
				task.run();
			}
		}
	}

}
